<?php
/**
 * 异步交互控制器
 * @author jianhui
 *
 */
class Ajax extends MY_Controller{
    function Ajax(){
        parent::MY_Controller();
    }
    /**
     * 载入年份列表
     */
    function loadYearList(){
        //时间单位配置
        $timeUnitList = $this->timeUnitModel->getCacheData('yearList');
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        foreach ($timeUnitList as $k => $v){
            $list[] = array('key'=>$k, 'value'=>$v);
        }
        echo jsonEncode($list);
        exit();
    }
    /**
     * 载入时段列表
     */
    function loadTimeUnitList(){
        $year = $this->input->post('year');
        //时间单位配置
        $timeUnitList = $this->timeUnitModel->getTimeUnitList($year);
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        foreach ($timeUnitList as $k => $v){
            $list[] = array('key'=>$k, 'value'=>$v);
        }
        echo jsonEncode($list);
        exit();
    }
    /**
     * 载入周列表
     */
    function loadWeekList(){
        $year = (int)$this->input->post('year');
        $timeUnit = (int)$this->input->post('timeUnit');
        //时间单位配置
        $weekList = $this->timeUnitModel->getWeekList($year, $timeUnit);
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        foreach ($weekList as $k => $v){
            $list[] = array('key'=>$k, 'value'=>$v);
        }
        echo jsonEncode($list);
        exit();
    }
    /**
     * 载入项目类型列表
     */
    function loadProTypeList(){
        $this->load->model('proTypeModel');
        $proTypeList = $this->proTypeModel->getCacheData('proTypeList');
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        foreach ($proTypeList as $k => $v){
            $list[] = array('key'=>(int)$v['proTypeId'], 'value'=>$v['proTypeName']);
        }
        echo jsonEncode($list);
        exit();
    }
    /**
     * 载入地区列表
     */
    function loadPlaceList(){
        $type = $this->input->post('type');
        $lastLevelValue = $this->input->post('lastLevelValue');
        $list = array();
        switch ($type){
            case 'area':
                $list = $this->userLib->getAreaList();
                array_unshift($list, array('key'=>'', 'value'=>'请选择'));
                break;
            case 'region':
                $lastLevelValue && $list = $this->userLib->getRegionList($lastLevelValue);
                array_unshift($list, array('key'=>'', 'value'=>'请选择'));
                break;
            case 'province':
                $lastLevelValue && $list = $this->userLib->getProvinceList($lastLevelValue);
                array_unshift($list, array('key'=>'', 'value'=>'请选择'));
                break;
            case 'city':
                $lastLevelValue && $list = $this->userLib->getCityList($lastLevelValue);
                array_unshift($list, array('key'=>'', 'value'=>'请选择'));
                break;
            case 'store':
                $lastLevelValue && $list = $this->userLib->getStoreList($lastLevelValue);
                array_unshift($list, array('key'=>'', 'value'=>'请选择'));
                break;
        }
        echo jsonEncode($list);
        exit();
    }
    /**
     * 搜索专营店列表
     */
    function searchStore(){
        //专营店筛选
        $area = $this->input->post('area');
        $region = $this->input->post('region');
        $spsarea = $this->input->post('spsarea');
        $province = $this->input->post('province');
        $city = $this->input->post('city');
        $keyword = $this->input->post('keyword');
        $fundType = (int)$this->input->post('fundType');
        //权限内专营店列表
        $storeId = 0;
        $viewStoreIds = $this->userLib->getViewStoreIds();
        $viewStoreIds != '*' && $storeId = $viewStoreIds;
        //如果为城市联合基金类型，则只搜索主单店
        if ($fundType == 2){
            $this->load->model('jurSellpointModel');
            $baichengIds = $this->jurSellpointModel->get100CityStoreIds();
            if ($storeId){
                $storeId = array_intersect((array)$storeId, (array)$baichengIds);
            }else{
                $storeId = $baichengIds;
            }
        }
        //查找对应地区列表
        $this->load->model('sellpointModel');
        $searchStoreList = $this->sellpointModel->searchStore($area, $region, $spsarea, $province, $city, $storeId, $keyword);
        $list = array();
        foreach($searchStoreList as $v){
            $list[] = array('key'=>(int)$v['userId'], 'value'=>$v['storeName']);
        }
        echo jsonEncode($list);
        exit();
    }
    /**
     * 载入项目状态类型
     */
    function loadProStatusList(){
        $proStatusList = $this->config->item('proStatus');
        $list = array();
        $list[] = array('key'=>'null','value'=>'请选择');
        foreach ($proStatusList as $k => $v){
            $list[] = array('key'=>$k, 'value'=>$v);
        }
        echo jsonEncode($list);
        exit();
    }
}